<?php

class Index extends Restricted_Controller {

	function __construct()
	{
		parent::__construct();	
		
		$this->load->model('db/views/page_content', 'vw_page_content');
	}
	
	private function loggedin_check()
	{
		$aLoginCss = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('generic'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$aLoginJs = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('jquery'),
							'version' => '1.0'
		);
		
		$this->user_logged_in('platform/'.$this->sPlatform.'/login', $aLoginJs, $aLoginCss, 'Heathrow Commuter - Library of Assets', false, 'bare');
	}
	
	public function index()
	{
		$this->load_page();
	}
	
	
	
	public function load_page($sPageName = null)
	{
		$this->loggedin_check();
		
		$this->vw_page_content->page_name = null;
		$this->vw_page_content->page_name = $sPageName;
		$this->vw_page_content->page_status = 'LI';
		
		$qPageContent = $this->vw_page_content->get(array(), 1);
	
		$this->display_page($qPageContent);	
	}
		
		
	
	function preview_page($iPageDataId) 
	{
		$this->loggedin_check();
		
		$this->vw_page_content->page_data_id = $iPageDataId;
		
		$qPageContent = $this->vw_page_content->get(1);
	
		$this->display_page($qPageContent);	
	}
	
	
	function display_page($qPageContent) 
	{
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('jquery', 'generic'),
							'version' => '1.0'
		);
		
		$aCss = array('generic');
		
		$this->load->library('user_agent');
		
		if($this->agent->browser() == 'Internet Explorer')
		{
			$aCss[] = 'ie_css';
			
			if($this->agent->version() == 6)
			{
				$aCss[] = 'ie_six_css';
			}
		}
		
		// build up our header_css array
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => $aCss,
							'media' => 'all',
							'version' => '1.0'
		);
		
		if ($qPageContent->num_rows() === 0)
		{
			$sRedirect = '/';
			
			$this->vw_page_content->site_id = false;
			
			$qPageContent = $this->vw_page_content->get(array(), 1);
			
			if ($qPageContent->num_rows() === 1)
			{
				$this->load->model('db/domains', 'domains');
				$this->load->model('db/languages', 'languages');
				
				$this->domains->clear();
				$this->domains->site_id = $qPageContent->row(0)->site_id;
				$this->domains->domain_type = $this->config->item('domain_type');
				$this->domains->domain_master = 'Y';
				
				$qDomain = $this->domains->get(array(), 1);
				
				if ($qDomain->num_rows() === 1)
				{
					$sRedirect = 'http://'.$qDomain->row(0)->domain_base.'/';
				}
				else 
				{
					$sRedirect = $this->config->item('site_default_domain');
				}
					
				if ($qPageContent->row(0)->page_name !== null)
				{
					$this->languages->lang_id = $qPageContent->row(0)->lang_id;
					
					$qLang = $this->languages->get(array(), 1);
					
					if ($qLang->num_rows() === 1)
					{
						$sRedirect .= $qLang->row(0)->lang_accr.'/'.$qPageContent->row(0)->page_name;
					}
				}
			}
			
			redirect($sRedirect, 'location', 301);
		}
		
		//get the page components
		$this->load->model('db/views/page_components', 'vw_page_components');
		$this->vw_page_components->component_status = 'LI';
		$this->vw_page_components->page_data_id = $qPageContent->row(0)->page_data_id;
		$qComponents = $this->vw_page_components->get();
		$aComponents = array();
		
		for($i = 0; $i < $qComponents->num_rows(); $i++)
		{
			if(!isset($aComponents[$qComponents->row($i)->component_position]))
			{
				$aComponents[$qComponents->row($i)->component_position] = array();
			}
			
			$aComponents[$qComponents->row($i)->component_position][] = $qComponents->row($i)->component;
		}
		
		$data['aComponents'] = $aComponents;
		
		//get the banner
		$this->load->model('db/views/page_banners', 'page_banners');
		$this->page_banners->banner_status = 'LI';
		$this->page_banners->page_data_id = $qPageContent->row(0)->page_data_id;
		$qBanner = $this->page_banners->get(1);
		
		$data['qBanner'] = $qBanner;
		
		// now get the top level page
		$this->load->model('db/page_parent_lookup', 'tbl_page_parent_lookup');
		
		$this->tbl_page_parent_lookup->page_id = $qPageContent->row(0)->page_id;
		
		// so find the top level page id
		$this->iTopLevelPageId = $this->tbl_page_parent_lookup->search_page_parent();

		//echo 'iTopLevelPageId: '.$this->iTopLevelPageId.'<br />iTopLevelSelectedPageId: '.$this->iTopLevelSelectedPageId.'<hr />';
		
		// then find the current page child
		$this->tbl_page_parent_lookup->page_id = $qPageContent->row(0)->page_id;

		$this->iTopLevelSelectedPageId = $this->tbl_page_parent_lookup->search_page_parent($qPageContent->row(0)->page_id, $this->iTopLevelPageId);
		
		$this->iPageId = $qPageContent->row(0)->page_id;
		$this->iPageDataId = $qPageContent->row(0)->page_data_id;
		$this->sPageKeywords = $qPageContent->row(0)->page_keywords;
		$this->sPageDesc = $qPageContent->row(0)->page_desc;
		$this->sPageTitle = $qPageContent->row(0)->page_title;
		
		$data['oPageContent'] = $qPageContent->row(0);

		$this->load->library('hooks');

		$this->hooks->_call_hook('pre_controller_view');
		
		$this->load->view('platform/'.$this->sPlatform.'/index', $data);
	}
	
	public function check_url($sUrl = '')
	{
		$sUrl = $this->uri->uri_string();

		if ((substr($sUrl, 0, 1) === '/') === true) 
		{
			$sUrl = substr($sUrl, 1);	
		}	
		
		if ((substr($sUrl, -1) === '/') === true) {
			
			$sUrl = substr($sUrl, 0, -1);	
		}
		
		$this->load->model('db/views/short_urls_by_site', 'vw_short_urls_by_site');
				
		$this->vw_short_urls_by_site->version_path = $sUrl;
		$this->vw_short_urls_by_site->version_active = 'Y';
		$this->vw_short_urls_by_site->page_status = 'LI';
				
		$qShortUrlPage = $this->vw_short_urls_by_site->get(array(), 1);
				
		if ($qShortUrlPage->num_rows() === 1)
		{
			$sUrl = '/';
			
			if ($qShortUrlPage->row(0)->page_name !== null) 
			{
				$sUrl = '/'.$qShortUrlPage->row(0)->lang_accr.'/'.$qShortUrlPage->row(0)->page_name;
			}
			
			redirect($sUrl, 'location', 301);
			exit;
		}
	}
	
	public function logout($sPath = '/')
	{
		$this->load->model('user');
		
		$this->user->clear_session();
		
		redirect($sPath, 'location');
	}
}


?>